//
//  GLFormBaseCell.m
//  KTools
//
//  Created by zhiyong.kuang on 2017/10/24.
//  Copyright © 2017年 zhiyong.kuang. All rights reserved.
//

#import "GLFormBaseCell.h"
#import "GLFormRow.h"
#import "GLForm.h"

#import "Masonry.h"

@interface GLFormBaseCell(){
}
@property(nonatomic,strong)UIView* tipV;
@property(nonatomic,strong)UIColor* tipVColor;
@end
@implementation GLFormBaseCell
@synthesize isRequired=_isRequired;
- (instancetype)init
{
    NSAssert(![self isMemberOfClass:[GLFormBaseCell class]], @"GLFormBaseCell is an abstract class, you should not instantiate it directly.");
    return [super init];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    NSAssert(![self isMemberOfClass:[GLFormBaseCell class]], @"GLFormBaseCell is an abstract class, you should not instantiate it directly.");

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _edgeMargin = 15;
        _contentMargin = 8;
        _glSeparatorInset = UIEdgeInsetsZero;
    }
    return self;
}

-(void)hideSeparator{
    self.glSeparatorInset = UIEdgeInsetsMake(0, [UIScreen mainScreen].bounds.size.width, 0, 0);
    [self resetSeparatorInset];
}
-(void)resetSeparatorInset{
    [self setSeparatorInset:self.glSeparatorInset];
    [self setLayoutMargins:self.glSeparatorInset];
}

//重写，避免badge背景色在cell选中时变透明
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    if (self.tipV) {
        self.tipVColor = self.tipV.backgroundColor;
    }
    [super setHighlighted:highlighted animated:animated];

    if (self.tipV) {
        self.tipV.backgroundColor = self.tipVColor;
    }
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    if (self.tipV) {
        self.tipVColor = self.tipV.backgroundColor;
    }
    [super setSelected:selected animated:animated];

    if (self.tipV) {
        self.tipV.backgroundColor = self.tipVColor;
    }
}



-(void)setIsRequired:(BOOL)isRequired{
    _isRequired = isRequired;
    if (_isRequired) {
        if (!self.tipV) {
            self.tipV = [[UIView alloc]init];

            self.tipV.backgroundColor = [UIColor  redColor];

            self.tipV.translatesAutoresizingMaskIntoConstraints = NO;
            CGFloat width = 4;
            CGFloat radius = width/2.0;
            self.tipV.layer.cornerRadius = radius;
            [self.contentView addSubview:self.tipV];
            
            [self.tipV mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.width.height.mas_equalTo(width);
                make.left.equalTo(self.contentView).offset(15/2.0-radius/2.0);
                make.top.equalTo(self.contentView).offset(20);
            }];
        }
        else{
            [self.contentView addSubview:self.tipV];
            [self.contentView bringSubviewToFront:self.tipV];
            self.tipV.hidden = NO;
        }
        
    }else{
        if (self.tipV) {
            self.tipV.hidden = YES;
        }
    }
}


-(void)valueDidChanged:(id)newValue{
    if (self.formRow) {
        self.formRow.value = newValue;
    }
 
}
-(void)setRowValue:(id)value{}

-(void)clicked:(id)sender subTag:(NSString*)tag{
    if (self.formRow.form.formClicked) {
        self.formRow.form.formClicked(self.formRow, sender,YES, tag);
    }
}
@end
